using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace FeatureWally.Client {
    public class FWSearch {
        private FWSearchType _type;
        private string _displayName;
        private Guid _id;
        private string _searchWord;
        private string _url;
        private Hashtable _featureDefinitions;

        public FWSearch(string searchword, string url) {
            _featureDefinitions = new Hashtable();
            _searchWord = searchword;
            _url = url;
            try {
                _id = new Guid(searchword);
                _type = FWSearchType.Id;
            }
            catch {
                _displayName = searchword.ToLower();
                _type = FWSearchType.DisplayName;
            }
        }

        public FWSearchType SearchType {
            get { return _type; }
        }
        public string SearchWord {
            get { return _searchWord; }
        }
        public string SearchUrl {
            get { return _url; }
        }
        public Hashtable FeatureDefinitions {
            get { return _featureDefinitions; }
        }

        public void AddActivation(SPFeature feature, string activatedAt) {
            if (!_featureDefinitions.ContainsKey(feature.DefinitionId)) {
                _featureDefinitions.Add(feature.DefinitionId, new FWFeatureDefinition(feature.Definition));
            }
            ((FWFeatureDefinition)_featureDefinitions[feature.DefinitionId]).ActivatedAt.Add(activatedAt);
        }
        public bool IsMatch(SPFeatureDefinition featureDefinition) {
            bool result = false;
            switch (_type) {
                case FWSearchType.Id:
                    if (featureDefinition != null) {
                        if (featureDefinition.Id != null) {
                            if (featureDefinition.Id.Equals(_id))
                                result = true;
                        }
                    }
                    break;
                case FWSearchType.DisplayName:
                    if (featureDefinition != null) {
                        if (featureDefinition.DisplayName != null) {
                            string name = string.Empty;
                            string title = string.Empty;
                            try {
                                name = featureDefinition.DisplayName.ToLower();
                            }
                            catch { }
                            try {
                                title = featureDefinition.GetTitle(System.Globalization.CultureInfo.CurrentCulture).ToLower();
                            }
                            catch { }

                            if (name != string.Empty)
                                if (name.Contains(_displayName))
                                    result = true;
                            if (title != string.Empty)
                                if (title.Contains(_displayName))
                                    result = true;
                        }
                    }
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
